/**
 * Class ContractsPage.
 * 
 * @author Jean-Luc Scheefer
 */
ContractsPage.inherits(Module);
function ContractsPage() {
	var that = this;
	
	// Inputs module
	this.inputClientID = new InputTextModule({
		'id' : 'inputClientID',
		'label' : I18n.get('contracts.client.identifier'),
		'placeholder' : I18n.get('contracts.client.identifier'),
		'validationPattern' : ValidationPattern.VALID_DEFAULT//TODO restrict to proper chars ?
	});
	
	// table
	this.cols = {};
	this.cols.clientId = new Column('clientId', I18n.get('contracts.client.identifier'), '20%', false);
	/*this.cols.clientId.getText = function(rc) { 
		return rc.clientId;
	};*/
	this.cols.mandateRUM = new Column('mandateRUM', I18n.get('contracts.client.mandateRUM'), '25%', true);
	this.cols.mandateRUM.getText = function(rc) {
		return '<a href="#MandatePage?mandateRum=' + encodeURIComponent(rc.mandateRUM) + '&creditorId=' + rc.creditorId + '">' + rc.mandateRUM + '</a>';
	};
	this.cols.mandateRUM.getAlt = function(rc) { 
		return rc.mandateRUM;
	};
	this.cols.contractId = new Column('cId', I18n.get('contracts.client.contractId'), '55%', true);
	this.cols.contractId.getText = function(rc) {
		var a = ' ', c = ' ';
		if (rc.cId.type == 'account'){
			a = rc.cId.id;
		} else {
			c = rc.cId.id;
		}
		return '<div style="min-height:15px;width:48%;float:left;">' + a + '</div>' +
			   '<div style="min-height:15px;width:48%;float:left;">' + c + '</div>';
	};
	this.cols.contractId.getAlt = function(rc) { 
		return I18n.get('contracts.contractId.' + rc.cId.type) + " : " + rc.cId.id;
	};
	this.cols.contractId.getTitle = function(){
		return '<div style="min-height:15px;width:48%;float:left;">' + I18n.get('contracts.client.accountId') + '</div>' +
		       '<div style="min-height:15px;width:48%;float:left;">' + I18n.get('contracts.client.contractId') + '</div>';
	};
	this.table = new TableSpanModule({
		'onSort' : function(){
			that.colorateTable();
		}
	});
	this.table.columnList.push(this.cols.clientId);
	this.table.columnList.push(this.cols.mandateRUM);
	this.table.columnList.push(this.cols.contractId);
	
}

ContractsPage.neededRoles = ["ROLE_USER", "VIEW_REPORTING", "VIEW_MANDATE", "VIEW_LPC_REPORTING"];

ContractsPage.prototype.fillJQ = function(jQ) {
	var that = this;
	
	// Title
	jQ.append($('<h4>' + I18n.get('contracts.pageTitle') + '</h4>'));
	
	// Form
	var form = $('<div class="form well row-fluid"></div>');
	jQ.append(form);
	var columnLeft = $('<div class="span6"></div>');
	columnLeft.append(this.inputClientID.buildJQ());
	form.append(columnLeft);
	// Buttons
	var buttonRow = $('<div class="btn-row"></div>');
	form.append(buttonRow);
	var buttonSearch = $('<div class="btn search">' + I18n.get('search') + '</div>');
	buttonSearch.click(function() {
		that.getMandates();
	});
	buttonRow.append(buttonSearch);
	
	//results table
	jQ.append(this.table.buildJQ());
};

ContractsPage.prototype.getMandates = function(){
	var that = this;
	AjaxManager.getJSON('getContractsAndAccounts', {
		'clientId' : this.inputClientID.value
	}, function(result) {
		if (result.success) {
			if (Util.checkObject(result.reports) && Util.checkArrayNotEmpty(result.reports)){
				that.table.objectMap = new Map();
				that.table.objectMap.compare['cId'] = function(a, b) {
					if (Util.check(a) && Util.check(b)) {
			        	if (a.id.toLowerCase() > b.id.toLowerCase()) {
			        		return 1;
			        	} else if (a.id.toLowerCase() < b.id.toLowerCase()) {
			        		return -1;
			        	}
			        }
			        return 0;
				};
				$.each(result.reports, function(id, val){
					that.table.objectMap.put(that.table.objectMap.size, val);
				});
				that.table.buildJQ();
				that.colorateTable();
			} else if (Util.checkObject(result.reports)){
				//id found but no contracts and account found
				PopupModule.getInstance().show({
					'title' : I18n.get('contracts.noContractsT'),
					'content' : I18n.get('contracts.noContracts'),
					'button0' : { 'text' : 'OK' }
				});
			} else {
				PopupModule.getInstance().show({
					'title' : I18n.get('contracts.noResultT'),
					'content' : I18n.get('contracts.noResult'),
					'button0' : { 'text' : 'OK' }
				});
			}
		}
	});
};

ContractsPage.prototype.colorateTable = function(){
	var bgColor = ["#F5F5F5", "#FBFBFB"], bgcIndex = 1;
	this.table.getJQ().find('tbody').find('tr').each(function(index, elem){
		if ($(elem).find('td').size() > 1){
			bgcIndex = (bgcIndex + 1) % 2;
		}
		$(elem).css('background-color', bgColor[bgcIndex]);
	});
};

